package com.yang.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.yang.demo.Constant;
import com.yang.demo.MainActivity;
import com.yang.demo.R;
import com.yang.demo.greendao.DaoManager;
import com.yang.demo.util.Loader;
import com.yang.demo.view.UserModel;

import java.util.List;
import java.util.jar.Manifest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yangwenwen on 2018/4/14.
 */
public class Login2Activity extends AppCompatActivity {

    @BindView(R.id.et_pwd)
    EditText etPWd;
    @BindView(R.id.et_account)
    EditText etAccount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login2);
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.main_blue), 50);
        StatusBarUtil.setDarkMode(this);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                Intent intent1 = new Intent(this, LoginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.tv_login:
                if (TextUtils.isEmpty(etAccount.getText().toString())) {
                    //未输入登录行号，提示用户
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.isEmpty(etPWd.getText().toString())) {
                    //未输入登录密码，提示用户
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                //验证账号和密码
                //取出数据库里的用户列表
                List<UserModel> userList = DaoManager.getInstance().getUserList();
                if (userList == null || userList.size() == 0) {
                    //用户列表为null或没有数据，则没有注册，提示用户先进行注册操作
                    Toast.makeText(this, "账号不存在,请先注册 ", Toast.LENGTH_LONG).show();
                } else {
                    //用户列表有数据
                    //设置一个tag，用于排查到账号和密码正确时进行记录
                    int tag = 0;
                    for (int i = 0; i < userList.size(); i++) {
                        //for循环用户列表
                        if (etAccount.getText().toString().equals(userList.get(i).getAccount())) {
                            //如果查询到用户名，继续比对密码
                            if (etPWd.getText().toString().endsWith(userList.get(i).getPassword())) {
                                //用户名和密码都正确，记录tag=1，表示账号和密码正确
                                tag = 1;
                                //记录用户名为全局变量，方便在后面取出操作
                                Constant.username = userList.get(i).getName();
                            }
                        }
                    }
                    if (tag == 0) {
                        Toast.makeText(this, "账号或密码错误", Toast.LENGTH_LONG).show();

                    } else {
                        //验证正确之后进入主页
                        Loader.show(this, "登陆中...");
                        Intent intent = new Intent();
                        intent.setClass(this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
                break;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
        return false;
    }

}
